package doit20.datayi.etl

import org.apache.spark.sql.{SaveMode, SparkSession}
import org.apache.spark.sql.expressions.UserDefinedFunction

object ApplogTrafficeCubeReport {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .appName("")
      .enableHiveSupport()
      .getOrCreate()

    import org.apache.spark.sql.functions._


    val table = spark.read.table("doit20dws.app_tfc_dimension_aggr_base").where("dt='2021-04-14'")
    table.createTempView("t")

    spark.udf.register("bitmap_aggr_card",udaf(BitMapAggrCard))

    val res = spark.sql(
      """
        |
        |select
        | appid          ,
        | appversion     ,
        | carrier        ,
        | devicetype     ,
        | ip             ,
        | nettype        ,
        | osname         ,
        | osversion      ,
        | releasechannel ,
        | resolution     ,
        | hour           ,
        | province       ,
        | city           ,
        | region         ,
        | isnew          ,
        | start_page     ,
        | end_page       ,
        | bitmap_aggr_card(uv_bitmap) as uv_cnt,
        | sum(pv_cnt) as pv_cnt,
        | sum(acctime_amt) as acctime_amt,
        | '2021-04-14' as dt
        |from
        |  t
        |group by
        | appid          ,
        | appversion     ,
        | carrier        ,
        | devicetype     ,
        | ip             ,
        | nettype        ,
        | osname         ,
        | osversion      ,
        | releasechannel ,
        | resolution     ,
        | hour           ,
        | province       ,
        | city           ,
        | region         ,
        | isnew          ,
        | start_page     ,
        | end_page
        |
        |grouping sets
        |(
        |   (),
        |   (isnew                 ),
        |   (appversion            ),
        |   (devicetype            ),
        |   (devicetype,isnew      ),
        |   (province              ),
        |   (province,isnew        ),
        |   (province,city,isnew   ),
        |   (province,city,region  ),
        |   (start_page            ),
        |   (start_page,isnew      ),
        |   (end_page              )
        |)
        |
        |""".stripMargin)


    res.write
      .mode(SaveMode.Append)
      .format("hive")
      .partitionBy("dt")
      .saveAsTable("doit20ads.app_tfc_cube_rpt")


    spark.close()
  }
}
